package cn.edu.gxnu.dsa.lab;

import java.util.Scanner;

import cn.edu.gxnu.dsa.list.SqList;

public class StudentInfoBook {
	SqList addressBook;

	public StudentInfoBook() {

	}

	public void start() {
		int opCode = 8;

		Scanner msc = new Scanner(System.in);
		while (opCode != 0) {
			welcomMsg();
			opCode = msc.nextInt();
			switch (opCode) {
			case 1:
				addressBook = createBook(addressBook, msc);
				break;
			case 2:
				if (addressBook == null) {
					System.out.println("!!!Please create your sequence List first!!!");
					break;
				}
				try {
					search(addressBook, msc);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 3:

				break;
			case 4:

				break;
			case 6:
				try {
					displayInfobook(addressBook);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case 7:
				opCode = 0;
			default:
				break;
			}
		}
	}

	private void welcomMsg() {
		System.out.println("+--------------------------------------------------+");
		System.out.println("|Lab:A student info book using singly linked list  |");
		System.out.println("|Author: TANG Zhiyong  Version: 0.1                |");
		System.out.println("|Eclipse Public License - v 1.0                    |");
		System.out.println("|Guangxi Normal University                         |");
		System.out.println("|                                                  |");
		System.out.println("|Please select the operation:                      |");
		System.out.println("|1 create a new student info book                  |");
		System.out.println("|2 find an item                                    |");
		System.out.println("|3 insert an item                                  |");
		System.out.println("|4 delete an item                                  |");
		System.out.println("|5 modify an item                                  |");
		System.out.println("|6 display the whole book                          |");
		System.out.println("|7 close the app                                   |");
		System.out.println("+--------------------------------------------------+");
	}

	private SqList createBook(SqList stuInfoBook, Scanner sc) {
		int maxSize;
		String stuNr, name, sex, telNr, address;

		if (stuInfoBook == null) {
			System.out.println("Please input the max size of the student info book:");
			maxSize = sc.nextInt();
			System.out.println("Please input how many items that you want to input now:");
			int listSize = sc.nextInt();
			stuInfoBook = new SqList(maxSize);
			for (int i = 0; i < listSize; i++) {
				System.out.println("Please input No." + (i + 1) + " student information followed the instruction:");
				System.out.println("Please input the student number:");
				stuNr = sc.next();
				System.out.println("Please input the student's name:");
				name = sc.next();
				System.out.println("Please input the student's sex:");
				sex = sc.next();
				System.out.println("Please input the student's phone number:");
				telNr = sc.next();
				System.out.println("Please input the student's address:");
				address = sc.next();
				Student student = new Student(stuNr, name, sex, telNr, address);
				try {
					stuInfoBook.insert(i, student);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
		} else {
			System.out.println("The student info book is not exist! Please do other operations.");
		}
		return stuInfoBook;
	}

	private void search(SqList stuInfoBook, Scanner sc) throws Exception {
		boolean found = false;
		System.out.println("Please select the index method: \n 1 by student number \n 2 by student name");
		int opCode = sc.nextInt();
		int i;

		if (opCode == 1) {
			System.out.println("Please input the student number:");
		} else if (opCode == 2) {
			System.out.println("Please input the student name:");
		} else {
			return;
		}

		String stuInfo = sc.next();

		for (i = 0; i < stuInfoBook.length(); i++) {
			if (opCode == 1) {
				if (((Student) stuInfoBook.get(i)).getStuNr().equals(stuInfo)) {
					found = true;
					break;
				}
			} else if (opCode == 2) {
				if (((Student) stuInfoBook.get(i)).getName().equals(stuInfo)) {
					found = true;
					break;
				}

			}
		}

		if (found) {
			((Student) stuInfoBook.get(i)).display(i);
		} else {
			System.out.println("The student is not found!");
		}

	}

	private void displayInfobook(SqList stuInfoBook) throws Exception {
		for (int i = 0; i < stuInfoBook.length(); i++) {
			((Student) stuInfoBook.get(i)).display(i);
		}
	}

}
